/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.controllers;

import com.aionengine.gameserver.controllers.observer.ActionObserver;
import com.aionengine.gameserver.controllers.observer.ObserverType;
import com.aionengine.gameserver.model.gameobjects.HouseObject;
import com.aionengine.gameserver.model.gameobjects.VisibleObject;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.templates.housing.PlaceableHouseObject;
import com.aionengine.gameserver.network.aion.serverpackets.SM_DELETE_HOUSE_OBJECT;
import com.aionengine.gameserver.network.aion.serverpackets.SM_HOUSE_OBJECT;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.utils.MathUtil;
import com.aionengine.gameserver.utils.PacketSendUtility;
import com.aionengine.gameserver.world.World;
import javolution.util.FastMap;

/**
 * @author Rolandas
 */
public class PlaceableObjectController<T extends PlaceableHouseObject> extends VisibleObjectController<HouseObject<T>> {

    FastMap<Integer, ActionObserver> observed = new FastMap<Integer, ActionObserver>().shared();

    @Override
    public void see(VisibleObject object) {
        Player p = (Player) object;
        ActionObserver observer = new ActionObserver(ObserverType.MOVE);
        p.getObserveController().addObserver(observer);
        observed.put(p.getObjectId(), observer);
        PacketSendUtility.sendPacket(p, new SM_HOUSE_OBJECT(getOwner()));
    }

    @Override
    public void notSee(VisibleObject object, boolean isOutOfRange) {
        Player p = (Player) object;
        ActionObserver observer = observed.remove(p.getObjectId());
        if (isOutOfRange) {
            observer.moved();
            PacketSendUtility.sendPacket(p, new SM_DELETE_HOUSE_OBJECT(getOwner().getObjectId()));
        }
        p.getObserveController().removeObserver(observer);
    }

    @Override
    public void onDespawn() {
        getOwner().onDespawn();
    }

    @Override
    public void delete() {
        if (getOwner().isSpawned())
            World.getInstance().despawn(getOwner(), false);
        World.getInstance().removeObject(getOwner());
    }

    public void onDialogRequest(Player player) {
        if (!MathUtil.isInRange(getOwner(), player, getOwner().getObjectTemplate().getTalkingDistance() + 2)) {
            PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_MSG_HOUSING_OBJECT_TOO_FAR_TO_USE);
            return;
        }
        getOwner().onDialogRequest(player);
    }
}
